package com.tuanzi.loan.web.lazy.statistics;

import java.util.Collections;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import com.tuanzi.loan.business.service.statistics.NginxLinkStatService;
import com.tuanzi.loan.core.lazy.BaseLazyDataModel;
import com.tuanzi.loan.core.util.SpringContextHolder;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.statistics.NginxLinkStatRequest;
import com.tuanzi.loan.vo.response.statistics.NginxLinkStatResponse;

/**
* @author weibifeng
*/
public class NginxLinkStatistisLazyDataModel extends BaseLazyDataModel<NginxLinkStatRequest, NginxLinkStatResponse> {

	private static final long serialVersionUID = -8524751302590740632L;
	private boolean isFirst = true;
	
	private NginxLinkStatService nginxLinkStatService = SpringContextHolder.getBean(NginxLinkStatService.class); 

	public NginxLinkStatistisLazyDataModel(NginxLinkStatRequest searchRequest) {
		super(searchRequest);
	}

	@Override
	protected Page<NginxLinkStatResponse> doLoad(
			PageSearcher<NginxLinkStatRequest> pageSearcher) {
		if (isFirst){
            isFirst = false;
            return new PageImpl<>(Collections.emptyList());
        }
		
		NginxLinkStatRequest searchRequest = pageSearcher.getSearchCondition();
		NginxLinkStatResponse sumResult = nginxLinkStatService.getAllListDataSum(searchRequest);
		searchRequest.setPvSum(sumResult.getPvSum());
		searchRequest.setUvSum(sumResult.getUvSum());
		return nginxLinkStatService.list(pageSearcher);
	}

}
